package cn.zxd.member.quote.dao;

import cn.zxd.member.quote.vo.*;

import java.util.Map;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;

/**
 * Created by DaoCreator (daoJdbc.jt)
 */
public class FeeJdbcDao implements FeeDao{
    private static Logger logger = Logger.getLogger(FeeJdbcDao.class);
    private Dao dao;

    public FeeJdbcDao(Dao dao){
        logger.debug("Dao :"+this.getClass().getName());
        this.dao = dao;
    }

    //#appendbegin
    //==============Fee_Def==============
    String FeeDef_QUERY_SQL = "select * from Fee_Def t ";

    private String createFeeDefFilter(Map<String, Object> param) {
        //to add filter,modify var filter
        String filter = " where 1=1 ";
        if (param.get("sn") != null && !param.get("sn").equals("")) {
            filter += " and t.sn = @sn ";
        }
        if (param.get("name") != null && !param.get("name").equals("")) {
            filter += " and t.name = @name ";
        }
        if (param.get("insFee") != null && !param.get("insFee").equals("")) {
            filter += " and t.insFee = @insFee ";
        }
        if (param.get("fee") != null && !param.get("fee").equals("")) {
            filter += " and t.fee = @fee ";
        }
        if (param.get("feeUnit") != null && !param.get("feeUnit").equals("")) {
            filter += " and t.feeUnit = @feeUnit ";
        }

        return filter;
    }

    private String createFeeDefOrderBy(String orderBy) {
        if (orderBy != null && !orderBy.equals(""))
            return " order by " + orderBy;
        else return "";
    }

    public void insertFeeDef(FeeDef vo) {
        dao.insert(vo);
    }

    public void updateFeeDef(FeeDef vo) {
        dao.update(vo);
    }

    public void deleteFeeDef(FeeDef vo) {
        dao.delete(vo);
    }

    public void deleteFeeDefByPrimaryKey(String sn ) {
        dao.delete(FeeDef.class, sn);
    }

    public void deleteFeeDefByParam(Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i<param.length; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        deleteFeeDefByParam(map);
    }

    public void deleteFeeDefByParam(Map<String, Object> param) {
        if (param == null) {
            param = new HashMap<String, Object>();
        }
        String filter = createFeeDefFilter(param);
        //don't delete when no filter
        if (!filter.trim().toLowerCase().equals("where 1=1")) {
            String sql = "delete from Fee_Def t " + filter;
            Sql s = Sqls.create(sql);
            s.params().putAll(param);
            dao.execute(s);
        } 
    }

    public FeeDef getFeeDefByPrimaryKey(String sn ) {
        String sql= FeeDef_QUERY_SQL + " where t.sn = @sn  ";
        Sql s = Sqls.create(sql);
        //todo:
        s.params().set("sn", sn);
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(FeeDef.class));
        dao.execute(s);
        return s.getObject(FeeDef.class);
    }

    public int getFeeDefCountByPrimaryKey(String sn ) {
        String sql="select count(sn) from Fee_Def t where t.sn = @sn   ";
        Sql s = Sqls.create(sql);
        //todo:
        s.params().set("sn", sn);
        s.setCallback(Sqls.callback.integer());
        dao.execute(s);
        return s.getInt();
    }

    public int getFeeDefCountByParam(Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i<param.length; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        return getFeeDefCountByParam(map);
    }

    public int getFeeDefCountByParam(Map<String, Object> param) {
        if (param == null) {
            param = new HashMap<String, Object>();
        }
        //to add fileter modify createFeeDefFilter
        String filter = createFeeDefFilter(param);
        String sql = "select count(sn) from Fee_Def t " + filter;
        Sql s = Sqls.create(sql);
        s.params().putAll(param);
        s.setCallback(Sqls.callback.integer());
        dao.execute(s);
        return s.getInt();
    }

    public List<FeeDef> getFeeDefListByParam(String orderBy, Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i < param.length ; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        return getFeeDefListByParam(orderBy, map);
    }

    public List<FeeDef> getFeeDefListByParam(int page, int count, String orderBy, Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i < param.length ; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        return getFeeDefListByParam(orderBy, map, page, count);
    }

    public List<FeeDef> getFeeDefListByParam(String orderBy, Map<String, Object> param) {
        if (param == null) {
            param = new HashMap<String, Object>();
        }
        //to add filter modify createFeeDefFilter
        String filter = createFeeDefFilter(param);
        String sql = "select * from (" + FeeDef_QUERY_SQL + filter + ") dataall " + createFeeDefOrderBy(orderBy);
        Sql s = Sqls.create(sql);
        s.params().putAll(param);
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(FeeDef.class));
        dao.execute(s);
        return s.getList(FeeDef.class);
    }

    public List<FeeDef> getFeeDefListByParam(String orderBy, Map<String, Object> param, int page, int count) {
        if (param == null) {
            param = new HashMap<String, Object>();
        }
        param.put("row0", (page - 1) * count);
        param.put("row1", count);

        //to add filter modify createFeeDefFilter
        String filter = createFeeDefFilter(param);
        String sql =  "select * from (" + FeeDef_QUERY_SQL + filter + ") dataall " + createFeeDefOrderBy(orderBy) + " limit @row0, @row1";

        Sql s = Sqls.create(sql);
        s.params().putAll(param);
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(FeeDef.class));
        dao.execute(s);
        return s.getList(FeeDef.class);


    }

    public List<FeeDef> getAllFeeDefList(String orderBy){
	    String sql = FeeDef_QUERY_SQL;
		Sql s = Sqls.create("select * from (" + sql + ") dataall " + createFeeDefOrderBy(orderBy));
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(FeeDef.class));
        dao.execute(s);
        return s.getList(FeeDef.class);
    }
    //==============end of Fee_Def==============
    

}
